import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
import { CreateSkuDto } from './dto/create-sku.dto';
import { UpdateSkuDto } from './dto/update-sku.dto';
import { InjectRepository } from '@nestjs/typeorm';
import { Sku } from './entities/sku.entity';
import { Repository } from 'typeorm';

@Injectable()
export class SkuService {
  constructor(
    @InjectRepository(Sku)
    private skuRepository: Repository<Sku>,
  ) {
  }

  async create(createSkuDto: CreateSkuDto) {
    const { img, price, stock, specCombination, goodId } = createSkuDto;
    const {identifiers} = await this.skuRepository.createQueryBuilder()
      .insert()
      .values({
        img,
        price,
        specCombination,
        stock,
        ...(goodId && { goodId })
      }).execute();
     if(identifiers.length===0){
       throw new HttpException("新增失败", HttpStatus.INTERNAL_SERVER_ERROR)
     }
     return identifiers[0]
  }

  findAll() {
    return `This action returns all sku`;
  }

  findOne(id: number) {
    return `This action returns a #${id} sku`;
  }

  update(id: number, updateSkuDto: UpdateSkuDto) {
    return `This action updates a #${id} sku`;
  }

  remove(id: number) {
    return `This action removes a #${id} sku`;
  }
}
